<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="Hexo">
  <link 
    rel="icon" 
    href="https://demo.theme-kaze.top/img/Kaze.png">
  <title>openresty使用lua脚本实现广告缓存.md</title>
  
    
      <meta 
        property="og:title" 
        content="openresty使用lua脚本实现广告缓存.md">
    
    
      <meta 
        property="og:url" 
        content="https://laoma.gitee.io/fungloengblog/2020/08/01/openresty%E4%BD%BF%E7%94%A8lua%E8%84%9A%E6%9C%AC%E5%AE%9E%E7%8E%B0%E5%B9%BF%E5%91%8A%E7%BC%93%E5%AD%98/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://demo.theme-kaze.top/img/Kaze.png">
    
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2020-08-01">
      <meta 
        property="og:article:modified_time" 
        content="2021-12-21">
      <meta 
        property="og:article:author" 
        content="IAMFUNGLOENG">
      
        
          <meta 
            property="og:article:tag" 
            content="openresty">
        
          <meta 
            property="og:article:tag" 
            content="lua">
        
      
    
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  
    <link rel="preload" href="/fungloengblog/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/fungloengblog/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/fungloengblog/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  <link rel="stylesheet" href="/fungloengblog/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
    <link rel="stylesheet" href="/fungloengblog/js/lib/lightbox/baguetteBox.min.css">
  
<meta name="generator" content="Hexo 5.4.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
        <img 
          class="navbar-logo-img"
          width="32"
          height="32"
          src="https://demo.theme-kaze.top/img/Kaze.png" 
          alt="blog logo">
      
      <span class="navbar-logo-dsc">FUNGLOENG</span>
    </span>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/fungloengblog/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/fungloengblog/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/fungloengblog/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/fungloengblog/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/fungloengblog/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/fungloengblog/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <a 
      class="navbar-menu-item darknavbar" 
      id="dark">
      <i class="iconfont icon-weather"></i>
    </a>
    <a 
      class="navbar-menu-item searchnavbar" 
      id="search">
      <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i>
    </a>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      openresty使用lua脚本实现广告缓存.md
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2020-08-01T04:10:36.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2020-08-01</span>
    </time>
    
    
      <span class="dot"></span>
      <span>1.8k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/fungloengblog/tags/openresty/" 
            class="post-meta-link">
            openresty
          </a>
        
          
            <span class="dot"></span>
          
          <a 
            href="/fungloengblog/tags/lua/" 
            class="post-meta-link">
            lua
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <h4 id="1-nginx配置缓存"><a href="#1-nginx配置缓存" class="headerlink" title="1.nginx配置缓存"></a>1.nginx配置缓存</h4><pre class="highlight"><span class="line"><span class="attr">lua_shared_dict</span> <span class="string">dis_cache 128m;</span></span><br></pre>

<p><img src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200801122912131.png" alt="image-20200801122912131" srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200801122912131.png" class="lozad post-image"></p>
<h4 id="2-在nginx中配置请求路径对应的lua处理脚本"><a href="#2-在nginx中配置请求路径对应的lua处理脚本" class="headerlink" title="2. 在nginx中配置请求路径对应的lua处理脚本"></a>2. 在nginx中配置请求路径对应的lua处理脚本</h4><pre class="highlight"><span class="line"><span class="attr">location</span> <span class="string">/read_content &#123;</span></span><br><span class="line">     <span class="attr">content_by_lua_file</span> <span class="string">/home/laoma/lua/read_content.lua;</span></span><br><span class="line"><span class="attr">&#125;</span></span><br></pre>

<p><img src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200801122932276.png" alt="image-20200801122932276" srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200801122932276.png" class="lozad post-image"></p>
<h4 id="3-lua脚本"><a href="#3-lua脚本" class="headerlink" title="3.lua脚本"></a>3.lua脚本</h4><p>在/home/laoma/lua目录下创建read_content.lua文件</p>
<pre class="highlight"><span class="line">ngx.header.content_type=<span class="string">&quot;application/json;charset=utf8&quot;</span></span><br><span class="line"><span class="keyword">local</span> uri_args = ngx.req.get_uri_args();</span><br><span class="line"><span class="keyword">local</span> id = uri_args[<span class="string">&quot;id&quot;</span>];</span><br><span class="line"><span class="comment">--获取本地缓存</span></span><br><span class="line"><span class="keyword">local</span> cache_ngx = ngx.shared.dis_cache;</span><br><span class="line"><span class="comment">--根据ID 获取本地缓存数据</span></span><br><span class="line"><span class="keyword">local</span> contentCache = cache_ngx:get(<span class="string">&#x27;content_cache_&#x27;</span>..id);</span><br><span class="line"><span class="keyword">if</span> contentCache == <span class="string">&quot;&quot;</span> <span class="keyword">or</span> contentCache == <span class="literal">nil</span> <span class="keyword">then</span></span><br><span class="line">	ngx.say(<span class="string">&quot;redis cache&quot;</span>);</span><br><span class="line">    <span class="keyword">local</span> redis = <span class="built_in">require</span>(<span class="string">&quot;resty.redis&quot;</span>);</span><br><span class="line">    <span class="keyword">local</span> red = redis:new()</span><br><span class="line">    red:set_timeout(<span class="number">2000</span>)</span><br><span class="line">    red:connect(<span class="string">&quot;127.0.0.1&quot;</span>, <span class="number">6379</span>)</span><br><span class="line">    <span class="keyword">local</span> rescontent=red:get(<span class="string">&quot;content_&quot;</span>..id);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> ngx.null == rescontent <span class="keyword">or</span> rescontent == <span class="literal">nil</span> <span class="keyword">or</span> rescontent == <span class="string">&quot;&quot;</span> <span class="keyword">then</span></span><br><span class="line">    	ngx.say(<span class="string">&quot;mysql&quot;</span>);</span><br><span class="line">        <span class="keyword">local</span> cjson = <span class="built_in">require</span>(<span class="string">&quot;cjson&quot;</span>);</span><br><span class="line">        <span class="keyword">local</span> mysql = <span class="built_in">require</span>(<span class="string">&quot;resty.mysql&quot;</span>);</span><br><span class="line">        <span class="keyword">local</span> db = mysql:new();</span><br><span class="line">        db:set_timeout(<span class="number">2000</span>)</span><br><span class="line">        <span class="keyword">local</span> props = &#123;</span><br><span class="line">            host = <span class="string">&quot;127.0.0.1&quot;</span>,</span><br><span class="line">            port = <span class="number">3306</span>,</span><br><span class="line">            database = <span class="string">&quot;loengmall_content&quot;</span>,</span><br><span class="line">            user = <span class="string">&quot;root&quot;</span>,</span><br><span class="line">            password = <span class="string">&quot;password&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">local</span> res = db:connect(props);</span><br><span class="line">        <span class="keyword">local</span> select_sql = <span class="string">&quot;select url,pic from tb_content where status =&#x27;1&#x27; and category_id=&quot;</span>..id..<span class="string">&quot; order by sort_order&quot;</span>;</span><br><span class="line">        res = db:query(select_sql);</span><br><span class="line">       </span><br><span class="line">        <span class="keyword">local</span> responsejson = cjson.encode(res);</span><br><span class="line">        red:set(<span class="string">&quot;content_&quot;</span>..id,responsejson);</span><br><span class="line">        ngx.say(responsejson);</span><br><span class="line">        db:<span class="built_in">close</span>()</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    	ngx.say(<span class="string">&quot;redis cache success&quot;</span>);</span><br><span class="line">        cache_ngx:set(<span class="string">&#x27;content_cache_&#x27;</span>..id, rescontent, <span class="number">2</span>*<span class="number">60</span>);</span><br><span class="line">        ngx.say(rescontent)</span><br><span class="line">    <span class="keyword">end</span></span><br><span class="line">    red:<span class="built_in">close</span>()</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    ngx.say(contentCache)</span><br><span class="line"><span class="keyword">end</span></span><br></pre>



<h4 id="4-nginx限流"><a href="#4-nginx限流" class="headerlink" title="4.nginx限流"></a>4.nginx限流</h4><h5 id="4-1-控制访问的速率"><a href="#4-1-控制访问的速率" class="headerlink" title="4.1 控制访问的速率"></a>4.1 控制访问的速率</h5><pre class="highlight"><span class="line"><span class="comment">#/usr/local/openresty/nginx/conf/nginx.conf文件</span></span><br><span class="line"><span class="section">http</span> &#123;</span><br><span class="line">    <span class="attribute">include</span>       mime.types;</span><br><span class="line">    <span class="attribute">default_type</span>  application/octet-stream;</span><br><span class="line"></span><br><span class="line">    <span class="comment">#cache</span></span><br><span class="line">    <span class="attribute">lua_shared_dict</span> dis_cache <span class="number">128m</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">#限流设置 binary_remote_addr根据请求IP进行限流 contentRateLimit:缓存空间名称 10m 缓存空间大小</span></span><br><span class="line">    <span class="comment"># rate=2r/s 速率；每秒钟允许有两个请求被处理</span></span><br><span class="line">    <span class="attribute">limit_req_zone</span> $binary_remote_addr zone=contentRateLimit:<span class="number">10m</span> rate=2r/s;</span><br><span class="line"></span><br><span class="line">    <span class="attribute">sendfile</span>        <span class="literal">on</span>;</span><br><span class="line">    <span class="comment">#tcp_nopush     on;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">#keepalive_timeout  0;</span></span><br><span class="line">    <span class="attribute">keepalive_timeout</span>  <span class="number">65</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">#gzip  on;</span></span><br><span class="line"></span><br><span class="line">    <span class="section">server</span> &#123;</span><br><span class="line">        <span class="attribute">listen</span>       <span class="number">80</span>;</span><br><span class="line">        <span class="attribute">server_name</span>  localhost;</span><br><span class="line"></span><br><span class="line">        <span class="attribute">location</span> /update_content &#123;</span><br><span class="line">            <span class="attribute">content_by_lua_file</span> /root/lua/update_content.lua;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="attribute">location</span> /read_content &#123;</span><br><span class="line">            <span class="comment">#使用限流配置</span></span><br><span class="line">            <span class="attribute">limit_req</span> zone=contentRateLimit ;</span><br><span class="line">            <span class="attribute">content_by_lua_file</span> /root/lua/read_content.lua;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre>



<p><img src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200802102308514.png" alt="image-20200802102308514" srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200802102308514.png" class="lozad post-image"></p>
<p><img src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200802102321783.png" alt="image-20200802102321783" srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200802102321783.png" class="lozad post-image"></p>
<p>使用改配置后，连续刷新会报错：</p>
<p><img src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200802102616736.png" alt="image-20200802102616736" srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200802102616736.png" class="lozad post-image"></p>
<p>处理突发流量</p>
<p>上面例子限制 2r/s，如果有时正常流量突然增大，超出的请求将被拒绝，无法处理突发流量，可以结合 <strong>burst</strong> 参数使用来解决该问题。</p>
<p>例如，如下配置表示：</p>
<pre class="highlight"><span class="line"><span class="attribute">location</span> /read_content &#123;</span><br><span class="line">           <span class="comment">#使用限流配置</span></span><br><span class="line">           <span class="attribute">limit_req</span> zone=contentRateLimit burst=<span class="number">4</span>;</span><br><span class="line">           <span class="attribute">content_by_lua_file</span> /root/lua/read_content.lua;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br></pre>



<p>burst 译为突发、爆发，表示在超过设定的处理速率后能额外处理的请求数,当 rate=10r/s 时，将1s拆成10份，即每100ms可处理1个请求。</p>
<p>此处，<strong>burst=4</strong> ，若同时有4个请求到达，Nginx 会处理第一个请求，剩余3个请求将放入队列，然后每隔500ms从队列中获取一个请求进行处理。若请求数大于4，将拒绝处理多余的请求，直接返回503.</p>
<p>不过，单独使用 burst 参数并不实用。假设 burst=50 ，rate依然为10r/s，排队中的50个请求虽然每100ms会处理一个，但第50个请求却需要等待 50 * 100ms即 5s，这么长的处理时间自然难以接受。</p>
<p>因此，burst 往往结合 nodelay 一起使用。</p>
<p>例如：如下配置：</p>
<pre class="highlight"><span class="line"><span class="attribute">location</span> /read_content &#123;</span><br><span class="line">           <span class="comment">#使用限流配置</span></span><br><span class="line">           <span class="attribute">limit_req</span> zone=contentRateLimit burst=<span class="number">4</span> nodelay;</span><br><span class="line">           <span class="attribute">content_by_lua_file</span> /root/lua/read_content.lua;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br></pre>

<p>如上表示：</p>
<p>平均每秒允许不超过2个请求，突发不超过4个请求，并且处理突发4个请求的时候，没有延迟，等到完成之后，按照正常的速率处理。</p>
<p>如上两种配置结合就达到了速率稳定，但突然流量也能正常处理的效果。完整配置代码如下：</p>
<pre class="highlight"><span class="line"><span class="attribute">user</span>  root root;</span><br><span class="line"><span class="attribute">worker_processes</span>  <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="section">events</span> &#123;</span><br><span class="line">    <span class="attribute">worker_connections</span>  <span class="number">1024</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="section">http</span> &#123;</span><br><span class="line">    <span class="attribute">include</span>       mime.types;</span><br><span class="line">    <span class="attribute">default_type</span>  application/octet-stream;</span><br><span class="line"></span><br><span class="line">    <span class="comment">#cache</span></span><br><span class="line">    <span class="attribute">lua_shared_dict</span> dis_cache <span class="number">128m</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">#限流设置</span></span><br><span class="line">    <span class="attribute">limit_req_zone</span> $binary_remote_addr zone=contentRateLimit:<span class="number">10m</span> rate=2r/s;</span><br><span class="line"></span><br><span class="line">    <span class="attribute">sendfile</span>        <span class="literal">on</span>;</span><br><span class="line">    <span class="comment">#tcp_nopush     on;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">#keepalive_timeout  0;</span></span><br><span class="line">    <span class="attribute">keepalive_timeout</span>  <span class="number">65</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">#gzip  on;</span></span><br><span class="line"></span><br><span class="line">    <span class="section">server</span> &#123;</span><br><span class="line">        <span class="attribute">listen</span>       <span class="number">80</span>;</span><br><span class="line">        <span class="attribute">server_name</span>  localhost;</span><br><span class="line"></span><br><span class="line">        <span class="attribute">location</span> /update_content &#123;</span><br><span class="line">            <span class="attribute">content_by_lua_file</span> /root/lua/update_content.lua;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="attribute">location</span> /read_content &#123;</span><br><span class="line">            <span class="attribute">limit_req</span> zone=contentRateLimit burst=<span class="number">4</span> nodelay;</span><br><span class="line">            <span class="attribute">content_by_lua_file</span> /root/lua/read_content.lua;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre>

<h5 id="5-2-控制并发量-连接数"><a href="#5-2-控制并发量-连接数" class="headerlink" title="5.2 控制并发量(连接数)"></a>5.2 控制并发量(连接数)</h5><p>(1) 配置限制固定的连接数</p>
<p>​    ngx_http_limit_conn_module 提供了限制连接数的能力。主要是利用limit_conn_zone和limit_conn两个指令。</p>
<p>利用连接数限制 某一个用户的ip连接的数量来控制流量。</p>
<p>注意：并非所有连接都被计算在内 只有当服务器正在处理请求并且已经读取了整个请求头时，才会计算有效连接。此处忽略测试。</p>
<p>具体配置如下：</p>
<pre class="highlight"><span class="line"><span class="section">http</span> &#123;</span><br><span class="line">    <span class="attribute">include</span>       mime.types;</span><br><span class="line">    <span class="attribute">default_type</span>  application/octet-stream;</span><br><span class="line"></span><br><span class="line">    <span class="comment">#cache</span></span><br><span class="line">    <span class="attribute">lua_shared_dict</span> dis_cache <span class="number">128m</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">#限流设置</span></span><br><span class="line">    <span class="attribute">limit_req_zone</span> $binary_remote_addr zone=contentRateLimit:<span class="number">10m</span> rate=2r/s;</span><br><span class="line"></span><br><span class="line">    <span class="comment">#根据IP地址来限制，存储内存大小10M</span></span><br><span class="line">    <span class="attribute">limit_conn_zone</span> $binary_remote_addr zone=addr:<span class="number">1m</span>;</span><br><span class="line"></span><br><span class="line">    <span class="attribute">sendfile</span>        <span class="literal">on</span>;</span><br><span class="line">    <span class="comment">#tcp_nopush     on;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">#keepalive_timeout  0;</span></span><br><span class="line">    <span class="attribute">keepalive_timeout</span>  <span class="number">65</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">#gzip  on;</span></span><br><span class="line"></span><br><span class="line">    <span class="section">server</span> &#123;</span><br><span class="line">        <span class="attribute">listen</span>       <span class="number">80</span>;</span><br><span class="line">        <span class="attribute">server_name</span>  localhost;</span><br><span class="line">        <span class="comment">#所有以brand开始的请求，访问本地changgou-service-goods微服务</span></span><br><span class="line">        <span class="attribute">location</span> /brand &#123;</span><br><span class="line">            <span class="attribute">limit_conn</span> addr <span class="number">2</span>;</span><br><span class="line">            <span class="attribute">proxy_pass</span> http://192.168.5.137:18081;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="attribute">location</span> /update_content &#123;</span><br><span class="line">            <span class="attribute">content_by_lua_file</span> /root/lua/update_content.lua;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="attribute">location</span> /read_content &#123;</span><br><span class="line">            <span class="attribute">limit_req</span> zone=contentRateLimit burst=<span class="number">4</span> nodelay;</span><br><span class="line">            <span class="attribute">content_by_lua_file</span> /root/lua/read_content.lua;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre>



<p>(2) 限制每个客户端IP与服务器的连接数，同时限制与虚拟服务器的总连接数.</p>
<p>如下配置:</p>
<pre class="highlight"><span class="line"><span class="comment">#存储个人请求IP的限流容量</span></span><br><span class="line"><span class="attribute">limit_conn_zone</span> $binary_remote_addr zone=perip:<span class="number">10m</span>;</span><br><span class="line"><span class="comment">#整个location对应的请求的并发容量配置</span></span><br><span class="line"><span class="attribute">limit_conn_zone</span> $server_name zone=perserver:<span class="number">10m</span>;</span><br><span class="line"><span class="section">server</span> &#123;  </span><br><span class="line">    <span class="attribute">listen</span>       <span class="number">80</span>;</span><br><span class="line">    <span class="attribute">server_name</span>  localhost;</span><br><span class="line">    <span class="attribute">charset</span> utf-<span class="number">8</span>;</span><br><span class="line">    <span class="attribute">location</span> / &#123;</span><br><span class="line">        <span class="attribute">limit_conn</span> perip <span class="number">10</span>;<span class="comment">#单个客户端ip与服务器的连接数．</span></span><br><span class="line">        <span class="attribute">limit_conn</span> perserver <span class="number">100</span>; ＃限制与服务器的总连接数</span><br><span class="line">        <span class="attribute">root</span>   html;</span><br><span class="line">        <span class="attribute">index</span>  index.html index.htm;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>













<h4 id="5-问题"><a href="#5-问题" class="headerlink" title="5. 问题"></a>5. 问题</h4><p>访问时候出现下面的问题：</p>
<p><img src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200801214052592.png" alt="image-20200801214052592" srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200801214052592.png" class="lozad post-image"></p>
<p>首先排查是否能访问上mysql数据，通过使用下面的lua脚本访问本地数据库发现，无法连接数据库，报以下错误：</p>
<pre class="highlight"><span class="line">Client does not support authentication protocol requested by server; consider upgrading MySQL client</span><br></pre>

<pre class="highlight"><span class="line">ngx.header.content_type=<span class="string">&quot;application/json;charset=utf8&quot;</span>				</span><br><span class="line"><span class="keyword">local</span> mysql = <span class="built_in">require</span> <span class="string">&quot;resty.mysql&quot;</span></span><br><span class="line">                <span class="keyword">local</span> db, err = mysql:new()</span><br><span class="line">                <span class="keyword">if</span> <span class="keyword">not</span> db <span class="keyword">then</span></span><br><span class="line">                    ngx.say(<span class="string">&quot;failed to instantiate mysql: &quot;</span>, err)</span><br><span class="line">                    <span class="keyword">return</span></span><br><span class="line">                <span class="keyword">end</span></span><br><span class="line"></span><br><span class="line">                db:set_timeout(<span class="number">1000</span>) <span class="comment">-- 1 sec</span></span><br><span class="line">                <span class="keyword">local</span> ok, err, errcode, sqlstate = db:connect&#123;</span><br><span class="line">                    host = <span class="string">&quot;127.0.0.1&quot;</span>,</span><br><span class="line">                    port = <span class="number">3306</span>,</span><br><span class="line">                    database = <span class="string">&quot;loengmall_content&quot;</span>,</span><br><span class="line">                    user = <span class="string">&quot;root&quot;</span>,</span><br><span class="line">                    password = <span class="string">&quot;password&quot;</span>,</span><br><span class="line">                    charset = <span class="string">&quot;utf8&quot;</span>,</span><br><span class="line">                    max_packet_size = <span class="number">1024</span> * <span class="number">1024</span>,</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                <span class="keyword">if</span> <span class="keyword">not</span> ok <span class="keyword">then</span></span><br><span class="line">                    ngx.say(<span class="string">&quot;failed to connect: &quot;</span>, err, <span class="string">&quot;: &quot;</span>, errcode, <span class="string">&quot; &quot;</span>, sqlstate)</span><br><span class="line">                    <span class="keyword">return</span></span><br><span class="line">                <span class="keyword">end</span></span><br><span class="line"></span><br><span class="line">                ngx.say(<span class="string">&quot;connected to mysql.&quot;</span>)</span><br><span class="line"></span><br><span class="line">                res, err, errcode, sqlstate =</span><br><span class="line">                    db:query(<span class="string">&quot;select url,pic from tb_content where status =&#x27;1&#x27; and category_id=&#x27;1&#x27; order by sort_order&quot;</span>, <span class="number">10</span>)</span><br><span class="line">                <span class="keyword">if</span> <span class="keyword">not</span> res <span class="keyword">then</span></span><br><span class="line">                    ngx.say(<span class="string">&quot;bad result: &quot;</span>, err, <span class="string">&quot;: &quot;</span>, errcode, <span class="string">&quot;: &quot;</span>, sqlstate, <span class="string">&quot;.&quot;</span>)</span><br><span class="line">                    <span class="keyword">return</span></span><br><span class="line">                <span class="keyword">end</span></span><br><span class="line">                <span class="keyword">local</span> cjson = <span class="built_in">require</span> <span class="string">&quot;cjson&quot;</span></span><br><span class="line">                ngx.say(<span class="string">&quot;result: &quot;</span>, cjson.encode(res))</span><br><span class="line">                <span class="comment">-- put it into the connection pool of size 100,</span></span><br><span class="line">                <span class="comment">-- with 10 seconds max idle timeout</span></span><br><span class="line">                <span class="keyword">local</span> ok, err = db:set_keepalive(<span class="number">10000</span>, <span class="number">100</span>)</span><br><span class="line">                <span class="keyword">if</span> <span class="keyword">not</span> ok <span class="keyword">then</span></span><br><span class="line">                    ngx.say(<span class="string">&quot;failed to set keepalive: &quot;</span>, err)</span><br><span class="line">                    <span class="keyword">return</span></span><br><span class="line">                <span class="keyword">end</span></span><br><span class="line"></span><br><span class="line">                <span class="comment">-- or just close the connection right away:</span></span><br><span class="line">                <span class="comment">-- local ok, err = db:close()</span></span><br><span class="line">                <span class="comment">-- if not ok then</span></span><br><span class="line">                <span class="comment">--     ngx.say(&quot;failed to close: &quot;, err)</span></span><br><span class="line">                <span class="comment">--     return</span></span><br><span class="line">                <span class="comment">-- end</span></span><br></pre>

<p>经过排查发现是以下问题：</p>
<pre class="highlight"><span class="line">出现连接失败的原因：mysql8 之前的版本中加密规则是mysql_native_password,而在mysql8之后,加密规则是caching_sha2_password。并提供了解决方案</span><br><span class="line">把用户密码登录的加密规则还原成mysql_native_password这种加密方式。</span><br></pre>

<pre class="highlight"><span class="line">#进入容器</span><br><span class="line">docker exec -it mysql /bin/bash</span><br><span class="line"></span><br><span class="line">#登录mysql服务</span><br><span class="line">mysql -h localhost -u root -p</span><br><span class="line"></span><br><span class="line">#选择数据库</span><br><span class="line">use mysql</span><br><span class="line">#查看加密方式</span><br><span class="line">select user,host,plugin from user where user=&#x27;root&#x27;;</span><br><span class="line"></span><br><span class="line">#修改加密方式</span><br><span class="line">ALTER USER &#x27;root&#x27;@&#x27;localhost&#x27; IDENTIFIED BY &#x27;password&#x27; PASSWORD EXPIRE NEVER;#这里的password为你在使用的数据库密码</span><br><span class="line">ALTER USER &#x27;root&#x27;@&#x27;localhost&#x27; IDENTIFIED WITH mysql_native_password BY &#x27;password&#x27;;#这里的password为你在使用的数据库密码</span><br><span class="line"></span><br><span class="line"> alter user &#x27;root&#x27;@&#x27;%&#x27; identified by &#x27;password&#x27; password expire never;</span><br><span class="line">alter user &#x27;root&#x27;@&#x27;%&#x27; identified with mysql_native_password by &#x27;password&#x27;;</span><br><span class="line">#确认加密方式</span><br><span class="line">select user,host,plugin from user where user=&#x27;root&#x27;;</span><br><span class="line"></span><br></pre>



<p><img src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200801214559422.png" alt="image-20200801214559422" srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200801214559422.png" class="lozad post-image"></p>
<p><img src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200801214755439.png" alt="image-20200801214755439" srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200801214755439.png" class="lozad post-image"></p>
<p>最终访问成功：</p>
<p><img src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200801214813152.png" alt="image-20200801214813152" srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200801214813152.png" class="lozad post-image"></p>
<p>解决了改问题后，成功访问：</p>
<p><img src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200801215825276.png" alt="image-20200801215825276" srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="C:\Users\laoma\AppData\Roaming\Typora\typora-user-images\image-20200801215825276.png" class="lozad post-image"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/fungloengblog/about">
            IAMFUNGLOENG
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://laoma.gitee.io/fungloengblog/2020/08/01/openresty%E4%BD%BF%E7%94%A8lua%E8%84%9A%E6%9C%AC%E5%AE%9E%E7%8E%B0%E5%B9%BF%E5%91%8A%E7%BC%93%E5%AD%98/">
            https://laoma.gitee.io/fungloengblog/2020/08/01/openresty%E4%BD%BF%E7%94%A8lua%E8%84%9A%E6%9C%AC%E5%AE%9E%E7%8E%B0%E5%B9%BF%E5%91%8A%E7%BC%93%E5%AD%98/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/fungloengblog/2020/08/03/MySQL%E5%BC%80%E5%90%AFbinlog%E6%97%A5%E5%BF%97%E6%A8%A1%E5%BC%8F/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">MySQL开启binlog日志模式 </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/fungloengblog/2020/07/28/docker%E8%BF%90%E8%A1%8Credis/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">docker运行redis.md </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-nginx%E9%85%8D%E7%BD%AE%E7%BC%93%E5%AD%98"><span class="toc-text">1.nginx配置缓存</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E5%9C%A8nginx%E4%B8%AD%E9%85%8D%E7%BD%AE%E8%AF%B7%E6%B1%82%E8%B7%AF%E5%BE%84%E5%AF%B9%E5%BA%94%E7%9A%84lua%E5%A4%84%E7%90%86%E8%84%9A%E6%9C%AC"><span class="toc-text">2. 在nginx中配置请求路径对应的lua处理脚本</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-lua%E8%84%9A%E6%9C%AC"><span class="toc-text">3.lua脚本</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-nginx%E9%99%90%E6%B5%81"><span class="toc-text">4.nginx限流</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#4-1-%E6%8E%A7%E5%88%B6%E8%AE%BF%E9%97%AE%E7%9A%84%E9%80%9F%E7%8E%87"><span class="toc-text">4.1 控制访问的速率</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-2-%E6%8E%A7%E5%88%B6%E5%B9%B6%E5%8F%91%E9%87%8F-%E8%BF%9E%E6%8E%A5%E6%95%B0"><span class="toc-text">5.2 控制并发量(连接数)</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-%E9%97%AE%E9%A2%98"><span class="toc-text">5. 问题</span></a></li></ol>
</div></main>
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://demo.theme-kaze.top/img/Kaze.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">IAMFUNGLOENG</p>
<p class="author-description">designed by fungloeng</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/fungloengblog/archives">
    <span>27</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/fungloengblog/categories">
    <span>8</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/fungloengblog/tags">
    <span>20</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-nginx%E9%85%8D%E7%BD%AE%E7%BC%93%E5%AD%98"><span class="toc-text">1.nginx配置缓存</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E5%9C%A8nginx%E4%B8%AD%E9%85%8D%E7%BD%AE%E8%AF%B7%E6%B1%82%E8%B7%AF%E5%BE%84%E5%AF%B9%E5%BA%94%E7%9A%84lua%E5%A4%84%E7%90%86%E8%84%9A%E6%9C%AC"><span class="toc-text">2. 在nginx中配置请求路径对应的lua处理脚本</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-lua%E8%84%9A%E6%9C%AC"><span class="toc-text">3.lua脚本</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-nginx%E9%99%90%E6%B5%81"><span class="toc-text">4.nginx限流</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#4-1-%E6%8E%A7%E5%88%B6%E8%AE%BF%E9%97%AE%E7%9A%84%E9%80%9F%E7%8E%87"><span class="toc-text">4.1 控制访问的速率</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-2-%E6%8E%A7%E5%88%B6%E5%B9%B6%E5%8F%91%E9%87%8F-%E8%BF%9E%E6%8E%A5%E6%95%B0"><span class="toc-text">5.2 控制并发量(连接数)</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-%E9%97%AE%E9%A2%98"><span class="toc-text">5. 问题</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/fungloengblog/categories/hexo/">
        <div class="categories-list-item">
          hexo
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/fungloengblog/categories/docker/">
        <div class="categories-list-item">
          docker
          <span class="categories-list-item-badge">2</span>
        </div>
      </a>
    
      <a href="/fungloengblog/categories/docker/fastdfs/">
        <div class="categories-list-item">
          docker/fastdfs
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/fungloengblog/categories/docker/mysql/">
        <div class="categories-list-item">
          docker/mysql
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/fungloengblog/categories/idea/">
        <div class="categories-list-item">
          idea
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/fungloengblog/categories/idea/github/">
        <div class="categories-list-item">
          idea/github
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/fungloengblog/categories/win10/">
        <div class="categories-list-item">
          win10
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/fungloengblog/categories/win10/hyper-v/">
        <div class="categories-list-item">
          win10/hyper-v
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/fungloengblog/tags/ubuntu/" 
        title="ubuntu">
        <div class="tags-list-item">ubuntu</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/docker/" 
        title="docker">
        <div class="tags-list-item">docker</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/openresty/" 
        title="openresty">
        <div class="tags-list-item">openresty</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/wsl2/" 
        title="wsl2">
        <div class="tags-list-item">wsl2</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/win10/" 
        title="win10">
        <div class="tags-list-item">win10</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/lua/" 
        title="lua">
        <div class="tags-list-item">lua</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/java/" 
        title="java">
        <div class="tags-list-item">java</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/canal/" 
        title="canal">
        <div class="tags-list-item">canal</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/MySQL/" 
        title="MySQL">
        <div class="tags-list-item">MySQL</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/wsl/" 
        title="wsl">
        <div class="tags-list-item">wsl</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/port/" 
        title="port">
        <div class="tags-list-item">port</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/hyper-v/" 
        title="hyper-v">
        <div class="tags-list-item">hyper-v</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/wsl1/" 
        title="wsl1">
        <div class="tags-list-item">wsl1</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/windows10/" 
        title="windows10">
        <div class="tags-list-item">windows10</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/gui/" 
        title="gui">
        <div class="tags-list-item">gui</div>
      </a>
    
      <a 
        href="/fungloengblog/tags/nginx/" 
        title="nginx">
        <div class="tags-list-item">nginx</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-nginx%E9%85%8D%E7%BD%AE%E7%BC%93%E5%AD%98"><span class="toc-text">1.nginx配置缓存</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E5%9C%A8nginx%E4%B8%AD%E9%85%8D%E7%BD%AE%E8%AF%B7%E6%B1%82%E8%B7%AF%E5%BE%84%E5%AF%B9%E5%BA%94%E7%9A%84lua%E5%A4%84%E7%90%86%E8%84%9A%E6%9C%AC"><span class="toc-text">2. 在nginx中配置请求路径对应的lua处理脚本</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-lua%E8%84%9A%E6%9C%AC"><span class="toc-text">3.lua脚本</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-nginx%E9%99%90%E6%B5%81"><span class="toc-text">4.nginx限流</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#4-1-%E6%8E%A7%E5%88%B6%E8%AE%BF%E9%97%AE%E7%9A%84%E9%80%9F%E7%8E%87"><span class="toc-text">4.1 控制访问的速率</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-2-%E6%8E%A7%E5%88%B6%E5%B9%B6%E5%8F%91%E9%87%8F-%E8%BF%9E%E6%8E%A5%E6%95%B0"><span class="toc-text">5.2 控制并发量(连接数)</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-%E9%97%AE%E9%A2%98"><span class="toc-text">5. 问题</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-12-21</div>
        <a href="/fungloengblog/2021/12/21/centos7%E6%90%AD%E5%BB%BA%E5%8D%9A%E5%AE%A2/"><div class="recent-posts-item-content">centos7搭建博客</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-12-21</div>
        <a href="/fungloengblog/2021/12/21/centos7%E5%8F%96%E6%B6%88%E8%87%AA%E5%8A%A8%E4%BC%91%E7%9C%A0/"><div class="recent-posts-item-content"></div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-12-21</div>
        <a href="/fungloengblog/2021/12/21/centos7%E5%AE%89%E8%A3%85docker/"><div class="recent-posts-item-content"></div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-12-21</div>
        <a href="/fungloengblog/2021/12/21/docker%E5%AE%89%E8%A3%85jenkins/"><div class="recent-posts-item-content"></div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2020 -
          
          2021
        </span>
        &nbsp;
        <a 
          href="/fungloengblog/" 
          class="footer-link">
          FUNGLOENG
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
      <div class="footer-dsc">
        
          本站总访问量<span id="busuanzi_value_site_pv"></span>次
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          本站总访客数<span id="busuanzi_value_site_uv"></span>次
        
      </div>
      
    
</footer> 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/fungloengblog/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/fungloengblog/js/main.js"></script> 
    
      <script> 
        loadScript('/fungloengblog/js/lib/busuanzi.min.js') 
      </script>
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/fungloengblog/js/lib/lozad.min.js', addLazyload)
      </script>
     
    
    
  </body>
</html>
